﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IRC.IRepairClouds.DataLayer.Cache
{
    public class TimeZoneCached
    {
        public int Id { get; private set; }
        public string Name { get; private set; }

        Settable<TimeZoneInfo> _timeZone;
        public TimeZoneInfo TimeZone
        {
            get
            {
                if (!_timeZone.IsSet)
                    _timeZone.Set(TimeZoneInfo.FindSystemTimeZoneById(Name));
                return _timeZone.Value;
            }
        }

        public ETimeZone CachedEntity(ICloudRepairEntities dbContext)
        {
            return dbContext.CachedETimeZone(Id);
        }

        public static IEnumerable<TimeZoneCached> CreateInitialLoad()
        {
            using (ICloudRepairEntities db = new ICloudRepairEntities())
            {
                foreach (var e in db.TimeZone.Select(i => new
                {
                    i.TimeZoneId,
                    i.TimeZoneName
                }))
                {
                    var ud = new TimeZoneCached();
                    ud.Id = e.TimeZoneId;
                    ud.Name = e.TimeZoneName;
                    yield return ud;
                }
            }
        }

        public override string ToString()
        {
            return string.Format("Id=[{0}] | Name=[{1}]", Id, Name);
        }

        public override bool Equals(object obj)
        {
            return (obj is TimeZoneCached) && ((obj as TimeZoneCached).Id == Id);
        }

        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }

        public static bool operator ==(TimeZoneCached a, TimeZoneCached b)
        {
            if (System.Object.ReferenceEquals(a, b))
                return true;
            if (((object)a == null) || ((object)b == null))
                return false;
            return a.Equals(b);
        }

        public static bool operator !=(TimeZoneCached a, TimeZoneCached b)
        {
            return !(a == b);
        }
    }

    public class TimeZoneCachedLoaderById : DataCachedLoader<TimeZoneCached>
    {
        public int Id { get; private set; }

        public TimeZoneCachedLoaderById(int id)
        {
            Id = id;
        }

        protected override TimeZoneCached Load()
        {
            return CacheManager.TimeZones[Id];
        }

        public override bool Equals(object obj)
        {
            return (obj is TimeZoneCachedLoaderById) && ((obj as TimeZoneCachedLoaderById).Id == Id);
        }

        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }

        public static bool operator ==(TimeZoneCachedLoaderById a, TimeZoneCachedLoaderById b)
        {
            if (System.Object.ReferenceEquals(a, b))
                return true;
            if (((object)a == null) || ((object)b == null))
                return false;
            return a.Equals(b);
        }

        public static bool operator !=(TimeZoneCachedLoaderById a, TimeZoneCachedLoaderById b)
        {
            return !(a == b);
        }
    }

    public class TimeZoneStaticCacheManager : StaticCacheManager<TimeZoneCached>
    {
        protected override IEnumerable<object[]> GetCachedKeys(TimeZoneCached obj)
        {
            yield return new object[] { obj.Id };
        }

        protected override void InitialLoad()
        {
            base.InsertInitialLoadBase(TimeZoneCached.CreateInitialLoad());
        }

        public TimeZoneCached this[int id]
        {
            get { return GetBase(id); }
        }
    }
}
